﻿Namespace PL.INVSDK
    Public Class InvCountHeader_PL
        Inherits PLBase
        Implements IPLProcess


        Public Property InvCountNoSEQ As Boolean = True

        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal queryName As String)
            MyBase.New(queryName)
        End Sub

        Public Overrides Function Delete() As Boolean
            Return True
        End Function

        Public Overrides Sub ValidateRow(row As DataRow)
            row.ClearErrors()
            'Mandatory Fields
            'TODO: Cache MetaString in AppManager, so here we read it once maybe for nothing
            'Dim ms As MetaString
            'ms = MetaString.GetMetaStringByCode("Mandatory", AppManager.ApplicationOption.LanguageId)
            If IsDBNull(row("InvCount_DT")) Then
                row.SetColumnError("InvCount_DT", "Mandatory")
            End If

            If IsDBNull(row("InvCountDescr_Prm")) Then
                row.SetColumnError("InvCountDescr_Prm", "Mandatory")
            End If

            If IsDBNull(row("FiscalYear")) Then
                row.SetColumnError("FiscalYear", "Mandatory")
            End If

            If IsDBNull(row("CountTypeID_FK")) Then
                row.SetColumnError("CountTypeID_FK", "Mandatory")
            End If
            '
            If row.HasErrors Then
                row.RowError = "Please check your columns, a validation error occur"
            End If
        End Sub
        ''' <summary>
        ''' A chaque ligne on passe par ici pour les CUD. 
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks>Attention ici le handle pourrait être un autre table</remarks>
        Public Sub OnRowUpdating(ByVal sender As Object, ByVal e As SqlRowUpdatingEventArgs) Handles MetaDataTable.OnRowUpdating
            'OutstandingIssues: Valider quelle table du dataset on traite.
            '
            Me.MetaDataTable.Adapter.InsertCommand.Parameters("@InvCountNoSEQ").Value = InvCountNoSEQ
            Me.MetaDataTable.Adapter.UpdateCommand.Parameters("@InvCountNoSEQ").Value = 0
        End Sub

        Public Overrides Function Validate() As Boolean
            Return True
        End Function

        '
        ' Ce code n'est plus utilisé a enlever a la fin aout 2012
        '   Deleter la Metaquery ResetInvCountDefault
        '   Ne pas deleter la SP elle est toujours utilisé
        '
        '
        'Public Shared Function SetDefaultCountSheet(ByVal invCountId As Guid) As Boolean
        '    Using mq As MetaQuery = MetaQuery.GetMetaQueryByName("InvCountHeader")
        '        mq.SQLWhere = String.Format("InvCountId = '{0}'", invCountId.ToString)
        '        mq.FillResultQuery()
        '        If mq.ResultQuery.Rows.Count = 1 Then
        '            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing
        '            '
        '            Try
        '                sqlcmd = MetaQuery.GetSPCmdByMetaName("ResetInvCountDefault")
        '                sqlcmd.Parameters("@HeaderID").Value = invCountId
        '                sqlcmd.ExecuteNonQuery()
        '                Return True
        '            Catch ex As Exception
        '                Throw
        '            Finally
        '                If sqlcmd IsNot Nothing Then
        '                    sqlcmd.Connection.Close()
        '                    sqlcmd.Dispose()
        '                End If
        '            End Try
        '        End If
        '        Return False
        '    End Using
        'End Function


        Public Shared Function GenerateInventoryBatch(ByVal invCountId As Guid) As Boolean
            Dim invCountDT As Date
            Using mq As MetaQuery = MetaQuery.GetMetaQueryByName("InvCountHeader")
                mq.SQLWhere = String.Format("InvCountId = '{0}'", invCountId.ToString)
                mq.FillResultQuery()
                If mq.ResultQuery.Rows.Count = 1 Then
                    invCountDT = mq.ResultQuery.Rows(0).Item("InvCount_DT")
                    Dim sqlcmd As SqlCommand = Nothing
                    '
                    Try
                        sqlcmd = MetaQuery.GetSPCmdByMetaName("GenerateInventoryBatch")
                        sqlcmd.Parameters("@InvCountID").Value = invCountId
                        sqlcmd.Parameters("@TranDate").Value = invCountDT
                        sqlcmd.Parameters("@InvBatchID").Value = Guid.NewGuid
                        sqlcmd.ExecuteNonQuery()
                        Return True
                    Catch ex As Exception
                        Throw
                    Finally
                        If sqlcmd IsNot Nothing Then
                            sqlcmd.Connection.Close()
                            sqlcmd.Dispose()
                        End If
                    End Try
                End If
                Return False
            End Using
        End Function

        Public Shared Function GenerateInventorySheet(ByVal invCountId As Guid) As Boolean
            Using mq As MetaQuery = MetaQuery.GetMetaQueryByName("InvCountHeader")
                mq.SQLWhere = String.Format("InvCountId = '{0}'", invCountId.ToString)
                mq.FillResultQuery()
                If mq.ResultQuery.Rows.Count = 1 Then
                    Dim sqlcmd As SqlCommand = Nothing
                    Try
                        If HasInventoryCountSheetDetail(invCountId) Then
                            DeleteInventoryCountSheetDetail(invCountId)
                        End If

                        sqlcmd = MetaQuery.GetSPCmdByMetaName("InsertInvCountDetails")
                        sqlcmd.Parameters("@InvCountID").Value = invCountId
                        sqlcmd.ExecuteNonQuery()
                        Return True
                    Catch ex As Exception
                        Throw ex
                    Finally
                        If sqlcmd IsNot Nothing Then
                            sqlcmd.Connection.Close()
                            sqlcmd.Dispose()
                        End If
                    End Try
                End If
                Return False
            End Using
        End Function

        Public Shared Function HasInventoryCountSheetDetail(ByVal invCountId As Guid) As Boolean
            Using mq As MetaQuery = MetaQuery.GetMetaQueryByName("CheckInvCountDetails")
                mq.SQLWhere = String.Format("InvCountId_FK = '{0}'", invCountId.ToString)
                mq.FillResultQuery()
                If mq.ResultQuery.Rows.Count >= 1 Then
                    Return True
                End If
                Return False
            End Using
        End Function

        Public Shared Sub DeleteInventoryCountSheetDetail(ByVal invCountId As Guid)
            Using mq As MetaQuery = MetaQuery.GetMetaQueryByName("DeleteInvCountDetails")
                mq.SQLWhere = String.Format("InvCountId_FK = '{0}'", invCountId.ToString)
                mq.ExecuteQuery()
            End Using
        End Sub

        Public Function BatchRecords(dataRow() As DataRow) As Boolean Implements IPLProcess.BatchRecords
            Return True
        End Function

        Public Function CurrentRecord(dataRow As DataRow) As Boolean Implements IPLProcess.CurrentRecord
            Return True
        End Function

        Public Function NewRecord(dataRow As DataRow) As Boolean Implements IPLProcess.NewRecord
            Return True
        End Function
    End Class
End Namespace